package _55_链表中环的入口结点;

/*
题目描述
给一个链表，若其中包含环，请找出该链表的环的入口结点，否则，输出null。
 */
class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}

public class Solution {
    public ListNode EntryNodeOfLoop(ListNode pHead) {
        if (pHead == null) return null;
        ListNode fast = pHead, slow = pHead;
        while (slow != null && fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
            //相遇后
            if (slow == fast) {
                slow = pHead;
                while(slow!=fast){
                    slow = slow.next;
                    fast = fast.next;
                    return slow;
                }
            }
        }

        return null;
    }
}
